<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LINE 1 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span id="local-6989586621679543335"></span><span id="local-6989586621679543333"></span><span>
</span><span id="line-1"></span><span class="hs-pragma">{-# LANGUAGE Trustworthy          #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE CPP                  #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude    #-}</span><span>
</span><span id="line-4"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns         #-}</span><span>
</span><span id="line-5"></span><span class="hs-pragma">{-# LANGUAGE GADTs                #-}</span><span>
</span><span id="line-6"></span><span class="hs-pragma">{-# LANGUAGE TypeSynonymInstances #-}</span><span>
</span><span id="line-7"></span><span class="hs-pragma">{-# LANGUAGE FlexibleInstances    #-}</span><span>
</span><span id="line-8"></span><span class="hs-pragma">{-# LANGUAGE FlexibleContexts     #-}</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- Whether there are identities depends on the platform</span><span>
</span><span id="line-10"></span><span class="hs-pragma">{-# OPTIONS_HADDOCK hide #-}</span><span>
</span><span id="line-11"></span><span>
</span><span id="line-12"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-14"></span><span class="hs-comment">-- Module      :  GHC.IO.Windows.Handle</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow, 2017</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- License     :  see libraries/base/LICENSE</span><span>
</span><span id="line-17"></span><span class="hs-comment">--</span><span>
</span><span id="line-18"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-19"></span><span class="hs-comment">-- Stability   :  internal</span><span>
</span><span id="line-20"></span><span class="hs-comment">-- Portability :  non-portable</span><span>
</span><span id="line-21"></span><span class="hs-comment">--</span><span>
</span><span id="line-22"></span><span class="hs-comment">-- Raw read/write operations on Windows Handles</span><span>
</span><span id="line-23"></span><span class="hs-comment">--</span><span>
</span><span id="line-24"></span><span class="hs-comment">-----------------------------------------------------------------------------</span><span>
</span><span id="line-25"></span><span>
</span><span id="line-26"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.IO.Windows.Handle</span><span>
</span><span id="line-27"></span><span> </span><span class="hs-special">(</span><span> </span><span class="hs-comment">-- * Basic Types</span><span>
</span><span id="line-28"></span><span>   </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-29"></span><span>   </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>   </span><span class="hs-identifier">IoHandle</span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>   </span><span class="hs-identifier">HANDLE</span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>   </span><span class="hs-identifier">Io</span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-33"></span><span>
</span><span id="line-34"></span><span>   </span><span class="hs-comment">-- * Utility functions</span><span>
</span><span id="line-35"></span><span>   </span><span class="hs-identifier">convertHandle</span><span class="hs-special">,</span><span>
</span><span id="line-36"></span><span>   </span><span class="hs-identifier">toHANDLE</span><span class="hs-special">,</span><span>
</span><span id="line-37"></span><span>   </span><span class="hs-identifier">fromHANDLE</span><span class="hs-special">,</span><span>
</span><span id="line-38"></span><span>   </span><span class="hs-identifier">handleToMode</span><span class="hs-special">,</span><span>
</span><span id="line-39"></span><span>   </span><span class="hs-identifier">optimizeFileAccess</span><span class="hs-special">,</span><span>
</span><span id="line-40"></span><span>
</span><span id="line-41"></span><span>   </span><span class="hs-comment">-- * Standard Handles</span><span>
</span><span id="line-42"></span><span>   </span><span class="hs-identifier">stdin</span><span class="hs-special">,</span><span>
</span><span id="line-43"></span><span>   </span><span class="hs-identifier">stdout</span><span class="hs-special">,</span><span>
</span><span id="line-44"></span><span>   </span><span class="hs-identifier">stderr</span><span class="hs-special">,</span><span>
</span><span id="line-45"></span><span>
</span><span id="line-46"></span><span>   </span><span class="hs-comment">-- * File utilities</span><span>
</span><span id="line-47"></span><span>   </span><span class="hs-identifier">openFile</span><span class="hs-special">,</span><span>
</span><span id="line-48"></span><span>   </span><span class="hs-identifier">openFileAsTemp</span><span class="hs-special">,</span><span>
</span><span id="line-49"></span><span>   </span><span class="hs-identifier">release</span><span>
</span><span id="line-50"></span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span class="hs-cpp">




#include &quot;windows_cconv.h&quot;
</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- Can't avoid these semantics leaks, they are base constructs</span><span>
</span><span id="line-58"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Data.Bits</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-operator">.|.</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-special">(</span><span class="hs-operator">.&amp;.</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">shiftL</span><span class="hs-special">)</span><span>
</span><span id="line-59"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Data.Functor</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-operator">&lt;$&gt;</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-60"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Data.Typeable</span><span>
</span><span id="line-61"></span><span>
</span><span id="line-62"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Base</span><span>
</span><span id="line-63"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Enum</span><span>
</span><span id="line-64"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Num</span><span>
</span><span id="line-65"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Real</span><span>
</span><span id="line-66"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.List</span><span>
</span><span id="line-67"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Word</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Word8</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Word16</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Word64</span><span class="hs-special">)</span><span>
</span><span id="line-68"></span><span>
</span><span id="line-69"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO</span><span> </span><span class="hs-keyword">hiding</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">mask</span><span class="hs-special">)</span><span>
</span><span id="line-70"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Buffer</span><span>
</span><span id="line-71"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.BufferedIO</span><span>
</span><span id="line-72"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="hs-identifier">GHC.IO.Device</span><span>
</span><span id="line-73"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Device</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">SeekMode</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODeviceType</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODevice</span><span class="hs-special">(</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">devType</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">setSize</span><span class="hs-special">)</span><span>
</span><span id="line-74"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Exception</span><span>
</span><span id="line-75"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.IOMode</span><span>
</span><span id="line-76"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Windows.Encoding</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">withGhcInternalToUTF16</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">withUTF16ToGhcInternal</span><span class="hs-special">)</span><span>
</span><span id="line-77"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Windows.Paths</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">getDevicePath</span><span class="hs-special">)</span><span>
</span><span id="line-78"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Handle.Internals</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">debugIO</span><span class="hs-special">)</span><span>
</span><span id="line-79"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IORef</span><span>
</span><span id="line-80"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Event.Windows</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">LPOVERLAPPED</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">withOverlapped</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IOResult</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-81"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.Ptr</span><span>
</span><span id="line-82"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.C</span><span>
</span><span id="line-83"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.Marshal.Array</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">pokeArray</span><span class="hs-special">)</span><span>
</span><span id="line-84"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.Marshal.Alloc</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">alloca</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">allocaBytes</span><span class="hs-special">)</span><span>
</span><span id="line-85"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.Marshal.Utils</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">with</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">fromBool</span><span class="hs-special">)</span><span>
</span><span id="line-86"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign.Storable</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Storable</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-87"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="hs-identifier">GHC.Event.Windows</span><span> </span><span class="hs-keyword">as</span><span> </span><span class="hs-identifier">Mgr</span><span>
</span><span id="line-88"></span><span>
</span><span id="line-89"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Windows</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">LPVOID</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">LPDWORD</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">DWORD</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">HANDLE</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">BOOL</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">LPCTSTR</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">ULONG</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">WORD</span><span class="hs-special">,</span><span>
</span><span id="line-90"></span><span>                    </span><span class="hs-identifier">UCHAR</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">failIf</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">iNVALID_HANDLE_VALUE</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">failWith</span><span class="hs-special">,</span><span>
</span><span id="line-91"></span><span>                    </span><span class="hs-identifier">failIfFalse_</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">getLastError</span><span class="hs-special">)</span><span>
</span><span id="line-92"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Text.Show</span><span>
</span><span id="line-93"></span><span>
</span><span id="line-94"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-95"></span><span class="hs-comment">-- The Windows IO device handles</span><span>
</span><span id="line-96"></span><span>
</span><span id="line-97"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">NativeHandle</span><span>
</span><span id="line-98"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span>
</span><span id="line-99"></span><span>
</span><span id="line-100"></span><span class="hs-comment">-- | Bit of a Hack, but we don't want every handle to have a cooked entry</span><span>
</span><span id="line-101"></span><span class="hs-comment">--   but all copies of the handles for which we do want one need to share</span><span>
</span><span id="line-102"></span><span class="hs-comment">--   the same value.</span><span>
</span><span id="line-103"></span><span class="hs-comment">--   We can't store it separately because we don't know when the handle will</span><span>
</span><span id="line-104"></span><span class="hs-comment">--   be destroyed or invalidated.</span><span>
</span><span id="line-105"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">IoHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-106"></span><span>  </span><span class="hs-identifier">NativeHandle</span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">getNativeHandle</span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IoHandle</span><span> </span><span class="hs-identifier">NativeHandle</span><span>
</span><span id="line-107"></span><span>  </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">getConsoleHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-108"></span><span>                   </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">cookedHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IORef</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-109"></span><span>                   </span><span class="hs-special">}</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IoHandle</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span>
</span><span id="line-110"></span><span>
</span><span id="line-111"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">IoHandle</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-112"></span><span>
</span><span id="line-113"></span><span class="hs-comment">-- | Convert a ConsoleHandle into a general FileHandle</span><span>
</span><span id="line-114"></span><span class="hs-comment">--   This will change which DeviceIO is used.</span><span>
</span><span id="line-115"></span><span class="hs-identifier">convertHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span>
</span><span id="line-116"></span><span class="hs-identifier">convertHandle</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromHANDLE</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">toHANDLE</span><span>
</span><span id="line-117"></span><span>
</span><span id="line-118"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-119"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">Show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-120"></span><span>  </span><span class="hs-identifier">show</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">toHANDLE</span><span>
</span><span id="line-121"></span><span>
</span><span id="line-122"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-123"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">Show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-124"></span><span>  </span><span class="hs-identifier">show</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">getConsoleHandle</span><span>
</span><span id="line-125"></span><span>
</span><span id="line-126"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-127"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">GHC.IO.Device.RawIO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-128"></span><span>  </span><span class="hs-identifier">read</span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">hwndRead</span><span>
</span><span id="line-129"></span><span>  </span><span class="hs-identifier">readNonBlocking</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">hwndReadNonBlocking</span><span>
</span><span id="line-130"></span><span>  </span><span class="hs-identifier">write</span><span>            </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">hwndWrite</span><span>
</span><span id="line-131"></span><span>  </span><span class="hs-identifier">writeNonBlocking</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">hwndWriteNonBlocking</span><span>
</span><span id="line-132"></span><span>
</span><span id="line-133"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-134"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">GHC.IO.Device.RawIO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-135"></span><span>  </span><span class="hs-identifier">read</span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">consoleRead</span><span>
</span><span id="line-136"></span><span>  </span><span class="hs-identifier">readNonBlocking</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">consoleReadNonBlocking</span><span>
</span><span id="line-137"></span><span>  </span><span class="hs-identifier">write</span><span>            </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">consoleWrite</span><span>
</span><span id="line-138"></span><span>  </span><span class="hs-identifier">writeNonBlocking</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">consoleWriteNonBlocking</span><span>
</span><span id="line-139"></span><span>
</span><span id="line-140"></span><span class="hs-comment">-- | Generalize a way to get and create handles.</span><span>
</span><span id="line-141"></span><span class="hs-keyword">class</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">GHC.IO.Device.RawIO</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODevice</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">BufferedIO</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Typeable</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">)</span><span>
</span><span id="line-142"></span><span>      </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-143"></span><span>  </span><span class="hs-identifier">toHANDLE</span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-144"></span><span>  </span><span class="hs-identifier">fromHANDLE</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-145"></span><span>  </span><span class="hs-identifier">isLockable</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-146"></span><span>  </span><span class="hs-identifier">setCooked</span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-147"></span><span>  </span><span class="hs-identifier">isCooked</span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-148"></span><span>
</span><span id="line-149"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-150"></span><span>  </span><span class="hs-identifier">toHANDLE</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">getNativeHandle</span><span>
</span><span id="line-151"></span><span>  </span><span class="hs-identifier">fromHANDLE</span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">NativeHandle</span><span>
</span><span id="line-152"></span><span>  </span><span class="hs-identifier">isLockable</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">True</span><span>
</span><span id="line-153"></span><span>  </span><span class="hs-identifier">setCooked</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">const</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">return</span><span>
</span><span id="line-154"></span><span>  </span><span class="hs-identifier">isCooked</span><span>   </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-155"></span><span>
</span><span id="line-156"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-157"></span><span>  </span><span class="hs-identifier">toHANDLE</span><span>         </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">getConsoleHandle</span><span>
</span><span id="line-158"></span><span>  </span><span class="hs-identifier">fromHANDLE</span><span> </span><span class="hs-identifier">h</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">unsafePerformIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">newIORef</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-159"></span><span>  </span><span class="hs-identifier">isLockable</span><span> </span><span class="hs-identifier">_</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-160"></span><span>  </span><span class="hs-identifier">setCooked</span><span>  </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">val</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-161"></span><span>    </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">writeIORef</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">cookedHandle</span><span> </span><span class="hs-identifier">h</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">val</span><span>
</span><span id="line-162"></span><span>       </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-163"></span><span>  </span><span class="hs-identifier">isCooked</span><span>   </span><span class="hs-identifier">h</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">readIORef</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">cookedHandle</span><span> </span><span class="hs-identifier">h</span><span class="hs-special">)</span><span>
</span><span id="line-164"></span><span>
</span><span id="line-165"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-166"></span><span class="hs-comment">-- The Windows IO device implementation</span><span>
</span><span id="line-167"></span><span>
</span><span id="line-168"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-169"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">GHC.IO.Device.IODevice</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-170"></span><span>  </span><span class="hs-identifier">ready</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_ready</span><span>
</span><span id="line-171"></span><span>  </span><span class="hs-identifier">close</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_close</span><span>
</span><span id="line-172"></span><span>  </span><span class="hs-identifier">isTerminal</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_is_console</span><span>
</span><span id="line-173"></span><span>  </span><span class="hs-identifier">isSeekable</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_is_seekable</span><span>
</span><span id="line-174"></span><span>  </span><span class="hs-identifier">seek</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_seek</span><span>
</span><span id="line-175"></span><span>  </span><span class="hs-identifier">tell</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_tell</span><span>
</span><span id="line-176"></span><span>  </span><span class="hs-identifier">getSize</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_get_size</span><span>
</span><span id="line-177"></span><span>  </span><span class="hs-identifier">setSize</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_set_size</span><span>
</span><span id="line-178"></span><span>  </span><span class="hs-identifier">setEcho</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_set_echo</span><span>
</span><span id="line-179"></span><span>  </span><span class="hs-identifier">getEcho</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_get_echo</span><span>
</span><span id="line-180"></span><span>  </span><span class="hs-identifier">setRaw</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_set_buffering</span><span>
</span><span id="line-181"></span><span>  </span><span class="hs-identifier">devType</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_dev_type</span><span>
</span><span id="line-182"></span><span>  </span><span class="hs-identifier">dup</span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_duplicate</span><span>
</span><span id="line-183"></span><span>
</span><span id="line-184"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-185"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">GHC.IO.Device.IODevice</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-186"></span><span>  </span><span class="hs-identifier">ready</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_ready</span><span>
</span><span id="line-187"></span><span>  </span><span class="hs-identifier">close</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_close</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">convertHandle</span><span>
</span><span id="line-188"></span><span>  </span><span class="hs-identifier">isTerminal</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_is_console</span><span>
</span><span id="line-189"></span><span>  </span><span class="hs-identifier">isSeekable</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_is_seekable</span><span>
</span><span id="line-190"></span><span>  </span><span class="hs-identifier">seek</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_console_seek</span><span>
</span><span id="line-191"></span><span>  </span><span class="hs-identifier">tell</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_console_tell</span><span>
</span><span id="line-192"></span><span>  </span><span class="hs-identifier">getSize</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_get_console_size</span><span>
</span><span id="line-193"></span><span>  </span><span class="hs-identifier">setSize</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_set_console_size</span><span>
</span><span id="line-194"></span><span>  </span><span class="hs-identifier">setEcho</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_set_echo</span><span>
</span><span id="line-195"></span><span>  </span><span class="hs-identifier">getEcho</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_get_echo</span><span>
</span><span id="line-196"></span><span>  </span><span class="hs-identifier">setRaw</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">console_set_buffering</span><span>
</span><span id="line-197"></span><span>  </span><span class="hs-identifier">devType</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_dev_type</span><span>
</span><span id="line-198"></span><span>  </span><span class="hs-identifier">dup</span><span>        </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">handle_duplicate</span><span>
</span><span id="line-199"></span><span>
</span><span id="line-200"></span><span class="hs-comment">-- Default sequential read buffer size.</span><span>
</span><span id="line-201"></span><span class="hs-comment">-- for Windows 8k seems to be the optimal</span><span>
</span><span id="line-202"></span><span class="hs-comment">-- buffer size.</span><span>
</span><span id="line-203"></span><span class="hs-identifier">dEFAULT_BUFFER_SIZE</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-204"></span><span class="hs-identifier">dEFAULT_BUFFER_SIZE</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">8192</span><span>
</span><span id="line-205"></span><span>
</span><span id="line-206"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-207"></span><span class="hs-comment">-- See libraries/base/GHC/IO/BufferedIO.hs</span><span>
</span><span id="line-208"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">BufferedIO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-209"></span><span>  </span><span class="hs-identifier">newBuffer</span><span> </span><span class="hs-identifier">_dev</span><span> </span><span class="hs-identifier">state</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">newByteBuffer</span><span> </span><span class="hs-identifier">dEFAULT_BUFFER_SIZE</span><span> </span><span class="hs-identifier">state</span><span>
</span><span id="line-210"></span><span>  </span><span class="hs-identifier">fillReadBuffer</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">readBuf'</span><span>
</span><span id="line-211"></span><span>  </span><span class="hs-identifier">fillReadBuffer0</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">readBufNonBlocking</span><span>
</span><span id="line-212"></span><span>  </span><span class="hs-identifier">flushWriteBuffer</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">writeBuf'</span><span>
</span><span id="line-213"></span><span>  </span><span class="hs-identifier">flushWriteBuffer0</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">writeBufNonBlocking</span><span>
</span><span id="line-214"></span><span>
</span><span id="line-215"></span><span class="hs-comment">-- | @since 4.11.0.0</span><span>
</span><span id="line-216"></span><span class="hs-comment">-- See libraries/base/GHC/IO/BufferedIO.hs</span><span>
</span><span id="line-217"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">BufferedIO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-218"></span><span>  </span><span class="hs-identifier">newBuffer</span><span> </span><span class="hs-identifier">_dev</span><span> </span><span class="hs-identifier">state</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">newByteBuffer</span><span> </span><span class="hs-identifier">dEFAULT_BUFFER_SIZE</span><span> </span><span class="hs-identifier">state</span><span>
</span><span id="line-219"></span><span>  </span><span class="hs-identifier">fillReadBuffer</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">readBuf'</span><span>
</span><span id="line-220"></span><span>  </span><span class="hs-identifier">fillReadBuffer0</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">readBufNonBlocking</span><span>
</span><span id="line-221"></span><span>  </span><span class="hs-identifier">flushWriteBuffer</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">writeBuf'</span><span>
</span><span id="line-222"></span><span>  </span><span class="hs-identifier">flushWriteBuffer0</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">writeBufNonBlocking</span><span>
</span><span id="line-223"></span><span>
</span><span id="line-224"></span><span>
</span><span id="line-225"></span><span class="hs-identifier">readBuf'</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Int</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">Word8</span><span class="hs-special">)</span><span>
</span><span id="line-226"></span><span class="hs-identifier">readBuf'</span><span> </span><span class="hs-identifier">hnd</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-227"></span><span>  </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;readBuf handle=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hnd</span><span class="hs-special">)</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot; &quot;</span><span> </span><span class="hs-operator">++</span><span>
</span><span id="line-228"></span><span>           </span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot;\n&quot;</span><span class="hs-special">)</span><span>
</span><span id="line-229"></span><span>  </span><span class="hs-special">(</span><span class="hs-identifier">r</span><span class="hs-special">,</span><span class="hs-identifier">buf'</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">readBuf</span><span> </span><span class="hs-identifier">hnd</span><span> </span><span class="hs-identifier">buf</span><span>
</span><span id="line-230"></span><span>  </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;after: &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">buf'</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot;\n&quot;</span><span class="hs-special">)</span><span>
</span><span id="line-231"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">r</span><span class="hs-special">,</span><span class="hs-identifier">buf'</span><span class="hs-special">)</span><span>
</span><span id="line-232"></span><span>
</span><span id="line-233"></span><span class="hs-identifier">writeBuf'</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Buffer</span><span> </span><span class="hs-identifier">Word8</span><span class="hs-special">)</span><span>
</span><span id="line-234"></span><span class="hs-identifier">writeBuf'</span><span> </span><span class="hs-identifier">hnd</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-235"></span><span>  </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-special">(</span><span class="hs-string">&quot;writeBuf handle=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hnd</span><span class="hs-special">)</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot; &quot;</span><span> </span><span class="hs-operator">++</span><span>
</span><span id="line-236"></span><span>           </span><span class="hs-identifier">summaryBuffer</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot;\n&quot;</span><span class="hs-special">)</span><span>
</span><span id="line-237"></span><span>  </span><span class="hs-identifier">writeBuf</span><span> </span><span class="hs-identifier">hnd</span><span> </span><span class="hs-identifier">buf</span><span>
</span><span id="line-238"></span><span>
</span><span id="line-239"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-240"></span><span class="hs-comment">-- Standard I/O handles</span><span>
</span><span id="line-241"></span><span>
</span><span id="line-242"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">StdHandleId</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-243"></span><span>
</span><span id="line-244"></span><span class="hs-identifier">sTD_INPUT_HANDLE</span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">StdHandleId</span><span>
</span><span id="line-245"></span><span class="hs-identifier">sTD_INPUT_HANDLE</span><span>   </span><span class="hs-glyph">=</span><span>  </span><span class="hs-number">4294967286</span><span>
</span><span id="line-246"></span><span class="hs-identifier">sTD_OUTPUT_HANDLE</span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">StdHandleId</span><span>
</span><span id="line-247"></span><span class="hs-identifier">sTD_OUTPUT_HANDLE</span><span>  </span><span class="hs-glyph">=</span><span>  </span><span class="hs-number">4294967285</span><span>
</span><span id="line-248"></span><span class="hs-identifier">sTD_ERROR_HANDLE</span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">StdHandleId</span><span>
</span><span id="line-249"></span><span class="hs-identifier">sTD_ERROR_HANDLE</span><span>   </span><span class="hs-glyph">=</span><span>  </span><span class="hs-number">4294967284</span><span>
</span><span id="line-250"></span><span>
</span><span id="line-251"></span><span class="hs-pragma">{-# LINE 249 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-249"></span><span>
</span><span id="line-250"></span><span class="hs-identifier">getStdHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">StdHandleId</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-251"></span><span class="hs-identifier">getStdHandle</span><span> </span><span class="hs-identifier">hid</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-252"></span><span>  </span><span class="hs-identifier">failIf</span><span> </span><span class="hs-special">(</span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">iNVALID_HANDLE_VALUE</span><span class="hs-special">)</span><span> </span><span class="hs-string">&quot;GetStdHandle&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">c_GetStdHandle</span><span> </span><span class="hs-identifier">hid</span><span>
</span><span id="line-253"></span><span>
</span><span id="line-254"></span><span class="hs-identifier">stdin</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">stdout</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">stderr</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span>
</span><span id="line-255"></span><span class="hs-identifier">stdin</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">unsafePerformIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">mkConsoleHandle</span><span> </span><span class="hs-operator">=&lt;&lt;</span><span> </span><span class="hs-identifier">getStdHandle</span><span> </span><span class="hs-identifier">sTD_INPUT_HANDLE</span><span>
</span><span id="line-256"></span><span class="hs-identifier">stdout</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">unsafePerformIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">mkConsoleHandle</span><span> </span><span class="hs-operator">=&lt;&lt;</span><span> </span><span class="hs-identifier">getStdHandle</span><span> </span><span class="hs-identifier">sTD_OUTPUT_HANDLE</span><span>
</span><span id="line-257"></span><span class="hs-identifier">stderr</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">unsafePerformIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">mkConsoleHandle</span><span> </span><span class="hs-operator">=&lt;&lt;</span><span> </span><span class="hs-identifier">getStdHandle</span><span> </span><span class="hs-identifier">sTD_ERROR_HANDLE</span><span>
</span><span id="line-258"></span><span>
</span><span id="line-259"></span><span class="hs-identifier">mkConsoleHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span class="hs-special">)</span><span>
</span><span id="line-260"></span><span class="hs-identifier">mkConsoleHandle</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-261"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">ref</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">newIORef</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-262"></span><span>       </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ref</span><span>
</span><span id="line-263"></span><span>
</span><span id="line-264"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-265"></span><span class="hs-comment">-- Some console internal types to detect EOF.</span><span>
</span><span id="line-266"></span><span>
</span><span id="line-267"></span><span class="hs-comment">-- ASCII Ctrl+D (EOT) character.  Typically used by Unix consoles.</span><span>
</span><span id="line-268"></span><span class="hs-comment">-- use for cross platform compatibility and to adhere to the ASCII standard.</span><span>
</span><span id="line-269"></span><span class="hs-identifier">acCtrlD</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-270"></span><span class="hs-identifier">acCtrlD</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0x04</span><span>
</span><span id="line-271"></span><span class="hs-comment">-- ASCII Ctrl+Z (SUB) character. Typically used by Windows consoles to denote</span><span>
</span><span id="line-272"></span><span class="hs-comment">-- EOT.  Use for compatibility with user expectations.</span><span>
</span><span id="line-273"></span><span class="hs-identifier">acCtrlZ</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-274"></span><span class="hs-identifier">acCtrlZ</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0x1A</span><span>
</span><span id="line-275"></span><span>
</span><span id="line-276"></span><span class="hs-comment">-- Mask to use to trigger ReadConsole input processing end.</span><span>
</span><span id="line-277"></span><span class="hs-identifier">acEotMask</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG</span><span>
</span><span id="line-278"></span><span class="hs-identifier">acEotMask</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="hs-number">1</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">acCtrlD</span><span class="hs-special">)</span><span> </span><span class="hs-operator">.|.</span><span> </span><span class="hs-special">(</span><span class="hs-number">1</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">shiftL</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">acCtrlZ</span><span class="hs-special">)</span><span>
</span><span id="line-279"></span><span>
</span><span id="line-280"></span><span class="hs-comment">-- Structure to hold the control character masks</span><span>
</span><span id="line-281"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">PCONSOLE_READCONSOLE_CONTROL</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span>
</span><span id="line-282"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span>
</span><span id="line-283"></span><span>  </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">crcNLength</span><span>           </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG</span><span>
</span><span id="line-284"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcNInitialChars</span><span>     </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG</span><span>
</span><span id="line-285"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcDwCtrlWakeupMask</span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG</span><span>
</span><span id="line-286"></span><span>  </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcDwControlKeyState</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG</span><span>
</span><span id="line-287"></span><span>  </span><span class="hs-special">}</span><span> </span><span class="hs-keyword">deriving</span><span> </span><span class="hs-identifier">Show</span><span>
</span><span id="line-288"></span><span>
</span><span id="line-289"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">Storable</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-290"></span><span>  </span><span class="hs-identifier">sizeOf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">const</span><span> </span><span class="hs-special">(</span><span class="hs-number">16</span><span class="hs-special">)</span><span>
</span><span id="line-291"></span><span class="hs-pragma">{-# LINE 291 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-291"></span><span>  </span><span class="hs-identifier">alignment</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">const</span><span> </span><span class="hs-number">4</span><span>
</span><span id="line-292"></span><span class="hs-pragma">{-# LINE 292 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-292"></span><span>  </span><span class="hs-identifier">poke</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-identifier">crc</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-293"></span><span>    </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span class="hs-special">)</span><span>           </span><span class="hs-identifier">buf</span><span>
</span><span id="line-294"></span><span class="hs-pragma">{-# LINE 294 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-294"></span><span>        </span><span class="hs-special">(</span><span class="hs-identifier">crcNLength</span><span>           </span><span class="hs-identifier">crc</span><span class="hs-special">)</span><span>
</span><span id="line-295"></span><span>    </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">4</span><span class="hs-special">)</span><span class="hs-special">)</span><span>     </span><span class="hs-identifier">buf</span><span>
</span><span id="line-296"></span><span class="hs-pragma">{-# LINE 296 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-296"></span><span>        </span><span class="hs-special">(</span><span class="hs-identifier">crcNInitialChars</span><span>     </span><span class="hs-identifier">crc</span><span class="hs-special">)</span><span>
</span><span id="line-297"></span><span>    </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span class="hs-special">)</span><span>  </span><span class="hs-identifier">buf</span><span>
</span><span id="line-298"></span><span class="hs-pragma">{-# LINE 298 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-298"></span><span>        </span><span class="hs-special">(</span><span class="hs-identifier">crcDwCtrlWakeupMask</span><span>  </span><span class="hs-identifier">crc</span><span class="hs-special">)</span><span>
</span><span id="line-299"></span><span>    </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">12</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">buf</span><span>
</span><span id="line-300"></span><span class="hs-pragma">{-# LINE 300 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-300"></span><span>        </span><span class="hs-special">(</span><span class="hs-identifier">crcDwControlKeyState</span><span> </span><span class="hs-identifier">crc</span><span class="hs-special">)</span><span>
</span><span id="line-301"></span><span>
</span><span id="line-302"></span><span>  </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">buf</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-303"></span><span>    </span><span class="hs-identifier">vNLength</span><span>           </span><span class="hs-glyph">&lt;-</span><span>
</span><span id="line-304"></span><span>      </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span class="hs-special">)</span><span>           </span><span class="hs-identifier">buf</span><span>
</span><span id="line-305"></span><span class="hs-pragma">{-# LINE 305 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-305"></span><span>    </span><span class="hs-identifier">vNInitialChars</span><span>     </span><span class="hs-glyph">&lt;-</span><span>
</span><span id="line-306"></span><span>      </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">4</span><span class="hs-special">)</span><span class="hs-special">)</span><span>     </span><span class="hs-identifier">buf</span><span>
</span><span id="line-307"></span><span class="hs-pragma">{-# LINE 307 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-307"></span><span>    </span><span class="hs-identifier">vDwCtrlWakeupMask</span><span>  </span><span class="hs-glyph">&lt;-</span><span>
</span><span id="line-308"></span><span>      </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span class="hs-special">)</span><span>  </span><span class="hs-identifier">buf</span><span>
</span><span id="line-309"></span><span class="hs-pragma">{-# LINE 309 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-309"></span><span>    </span><span class="hs-identifier">vDwControlKeyState</span><span> </span><span class="hs-glyph">&lt;-</span><span>
</span><span id="line-310"></span><span>      </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">12</span><span class="hs-special">)</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">buf</span><span>
</span><span id="line-311"></span><span class="hs-pragma">{-# LINE 311 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-311"></span><span>    </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span> </span><span class="hs-special">{</span><span>
</span><span id="line-312"></span><span>        </span><span class="hs-identifier">crcNLength</span><span>           </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">vNLength</span><span class="hs-special">,</span><span>
</span><span id="line-313"></span><span>        </span><span class="hs-identifier">crcNInitialChars</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">vNInitialChars</span><span class="hs-special">,</span><span>
</span><span id="line-314"></span><span>        </span><span class="hs-identifier">crcDwCtrlWakeupMask</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">vDwCtrlWakeupMask</span><span class="hs-special">,</span><span>
</span><span id="line-315"></span><span>        </span><span class="hs-identifier">crcDwControlKeyState</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">vDwControlKeyState</span><span>
</span><span id="line-316"></span><span>      </span><span class="hs-special">}</span><span>
</span><span id="line-317"></span><span>
</span><span id="line-318"></span><span class="hs-comment">-- Create CONSOLE_READCONSOLE_CONTROL for breaking on control characters</span><span>
</span><span id="line-319"></span><span class="hs-comment">-- specified by acEotMask</span><span>
</span><span id="line-320"></span><span class="hs-identifier">eotControl</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span>
</span><span id="line-321"></span><span class="hs-identifier">eotControl</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-322"></span><span>  </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span>
</span><span id="line-323"></span><span>    </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">crcNLength</span><span>           </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-324"></span><span>                               </span><span class="hs-identifier">sizeOf</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">undefined</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CONSOLE_READCONSOLE_CONTROL</span><span class="hs-special">)</span><span>
</span><span id="line-325"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcNInitialChars</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-326"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcDwCtrlWakeupMask</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">acEotMask</span><span>
</span><span id="line-327"></span><span>    </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">crcDwControlKeyState</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-328"></span><span>    </span><span class="hs-special">}</span><span>
</span><span id="line-329"></span><span>
</span><span id="line-330"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">PINPUT_RECORD</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-331"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-332"></span><span class="hs-comment">-- Foreign imports</span><span>
</span><span id="line-333"></span><span>
</span><span id="line-334"></span><span>
</span><span id="line-335"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h CreateFileW&quot;</span><span>
</span><span id="line-336"></span><span>    </span><span class="hs-identifier">c_CreateFile</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPCTSTR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPSECURITY_ATTRIBUTES</span><span>
</span><span id="line-337"></span><span>                 </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-338"></span><span>                 </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-339"></span><span>
</span><span id="line-340"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h SetFileCompletionNotificationModes&quot;</span><span>
</span><span id="line-341"></span><span>    </span><span class="hs-identifier">c_SetFileCompletionNotificationModes</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">UCHAR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-342"></span><span>
</span><span id="line-343"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h ReadFile&quot;</span><span>
</span><span id="line-344"></span><span>    </span><span class="hs-identifier">c_ReadFile</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPVOID</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPDWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span>
</span><span id="line-345"></span><span>               </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-346"></span><span>
</span><span id="line-347"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h WriteFile&quot;</span><span>
</span><span id="line-348"></span><span>    </span><span class="hs-identifier">c_WriteFile</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPVOID</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPDWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span>
</span><span id="line-349"></span><span>                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-350"></span><span>
</span><span id="line-351"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h GetStdHandle&quot;</span><span>
</span><span id="line-352"></span><span>    </span><span class="hs-identifier">c_GetStdHandle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">StdHandleId</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-353"></span><span>
</span><span id="line-354"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__handle_ready&quot;</span><span>
</span><span id="line-355"></span><span>    </span><span class="hs-identifier">c_handle_ready</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CInt</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CInt</span><span>
</span><span id="line-356"></span><span>
</span><span id="line-357"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__is_console&quot;</span><span>
</span><span id="line-358"></span><span>    </span><span class="hs-identifier">c_is_console</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-359"></span><span>
</span><span id="line-360"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_console_buffering&quot;</span><span>
</span><span id="line-361"></span><span>    </span><span class="hs-identifier">c_set_console_buffering</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-362"></span><span>
</span><span id="line-363"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_console_echo&quot;</span><span>
</span><span id="line-364"></span><span>    </span><span class="hs-identifier">c_set_console_echo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-365"></span><span>
</span><span id="line-366"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__get_console_echo&quot;</span><span>
</span><span id="line-367"></span><span>    </span><span class="hs-identifier">c_get_console_echo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-368"></span><span>
</span><span id="line-369"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__close_handle&quot;</span><span>
</span><span id="line-370"></span><span>    </span><span class="hs-identifier">c_close_handle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-371"></span><span>
</span><span id="line-372"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__handle_type&quot;</span><span>
</span><span id="line-373"></span><span>    </span><span class="hs-identifier">c_handle_type</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-374"></span><span>
</span><span id="line-375"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_file_pointer&quot;</span><span>
</span><span id="line-376"></span><span>  </span><span class="hs-identifier">c_set_file_pointer</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-377"></span><span>
</span><span id="line-378"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__get_file_pointer&quot;</span><span>
</span><span id="line-379"></span><span>  </span><span class="hs-identifier">c_get_file_pointer</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CLong</span><span>
</span><span id="line-380"></span><span>
</span><span id="line-381"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__get_file_size&quot;</span><span>
</span><span id="line-382"></span><span>  </span><span class="hs-identifier">c_get_file_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CLong</span><span>
</span><span id="line-383"></span><span>
</span><span id="line-384"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_file_size&quot;</span><span>
</span><span id="line-385"></span><span>  </span><span class="hs-identifier">c_set_file_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-386"></span><span>
</span><span id="line-387"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__duplicate_handle&quot;</span><span>
</span><span id="line-388"></span><span>  </span><span class="hs-identifier">c_duplicate_handle</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-389"></span><span>
</span><span id="line-390"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_console_pointer&quot;</span><span>
</span><span id="line-391"></span><span>  </span><span class="hs-identifier">c_set_console_pointer</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-392"></span><span>
</span><span id="line-393"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__get_console_pointer&quot;</span><span>
</span><span id="line-394"></span><span>  </span><span class="hs-identifier">c_get_console_pointer</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CLong</span><span>
</span><span id="line-395"></span><span>
</span><span id="line-396"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__get_console_buffer_size&quot;</span><span>
</span><span id="line-397"></span><span>  </span><span class="hs-identifier">c_get_console_buffer_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CLong</span><span>
</span><span id="line-398"></span><span>
</span><span id="line-399"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;__set_console_buffer_size&quot;</span><span>
</span><span id="line-400"></span><span>  </span><span class="hs-identifier">c_set_console_buffer_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CLong</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-401"></span><span>
</span><span id="line-402"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h ReadConsoleW&quot;</span><span>
</span><span id="line-403"></span><span>  </span><span class="hs-identifier">c_read_console</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word16</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-404"></span><span>                 </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">PCONSOLE_READCONSOLE_CONTROL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-405"></span><span>
</span><span id="line-406"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h WriteConsoleW&quot;</span><span>
</span><span id="line-407"></span><span>  </span><span class="hs-identifier">c_write_console</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word16</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-408"></span><span>                  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-409"></span><span>
</span><span id="line-410"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h ReadConsoleInputW&quot;</span><span>
</span><span id="line-411"></span><span>  </span><span class="hs-identifier">c_read_console_input</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">PINPUT_RECORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPDWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-412"></span><span>
</span><span id="line-413"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">LPSECURITY_ATTRIBUTES</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">LPVOID</span><span>
</span><span id="line-414"></span><span>
</span><span id="line-415"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-416"></span><span class="hs-comment">-- Reading and Writing</span><span>
</span><span id="line-417"></span><span>
</span><span id="line-418"></span><span class="hs-comment">-- For this to actually block, the file handle must have</span><span>
</span><span id="line-419"></span><span class="hs-comment">-- been created with FILE_FLAG_OVERLAPPED not set. As an implementation note I</span><span>
</span><span id="line-420"></span><span class="hs-comment">-- am choosing never to let this block. But this can be easily accomplished by</span><span>
</span><span id="line-421"></span><span class="hs-comment">-- a getOverlappedResult call with True</span><span>
</span><span id="line-422"></span><span class="hs-identifier">hwndRead</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-423"></span><span class="hs-identifier">hwndRead</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-424"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fmap</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Mgr.withException</span><span> </span><span class="hs-string">&quot;hwndRead&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-425"></span><span>      </span><span class="hs-identifier">withOverlapped</span><span> </span><span class="hs-string">&quot;hwndRead&quot;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">ptr</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">completionCB</span><span>
</span><span id="line-426"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-427"></span><span>    </span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">outBuf</span><span> </span><span class="hs-identifier">lpOverlapped</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-428"></span><span>      </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: hwndRead&quot;</span><span>
</span><span id="line-429"></span><span>      </span><span class="hs-comment">-- See Note [ReadFile/WriteFile].</span><span>
</span><span id="line-430"></span><span>      </span><span class="hs-identifier">ret</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_ReadFile</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">outBuf</span><span class="hs-special">)</span><span>
</span><span id="line-431"></span><span>                        </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">bytes</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">nullPtr</span><span> </span><span class="hs-identifier">lpOverlapped</span><span>
</span><span id="line-432"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Mgr.CbNone</span><span> </span><span class="hs-identifier">ret</span><span>
</span><span id="line-433"></span><span>
</span><span id="line-434"></span><span>    </span><span class="hs-identifier">completionCB</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-435"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-436"></span><span class="hs-pragma">{-# LINE 436 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-436"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">38</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-437"></span><span class="hs-pragma">{-# LINE 437 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-437"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1073741807</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-438"></span><span class="hs-pragma">{-# LINE 438 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-438"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">109</span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-439"></span><span class="hs-pragma">{-# LINE 439 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-439"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1073741493</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-440"></span><span class="hs-pragma">{-# LINE 440 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-440"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">259</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-441"></span><span class="hs-pragma">{-# LINE 441 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-441"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">234</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-442"></span><span class="hs-pragma">{-# LINE 442 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-442"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>                           </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioFailed</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-443"></span><span>
</span><span id="line-444"></span><span class="hs-comment">-- In WinIO we'll never block in the FFI call, so this call is equivalent to</span><span>
</span><span id="line-445"></span><span class="hs-comment">-- hwndRead,  Though we may revisit this when implementing sockets and pipes.</span><span>
</span><span id="line-446"></span><span class="hs-comment">-- It still won't block, but may set up extra book keeping so threadWait and</span><span>
</span><span id="line-447"></span><span class="hs-comment">-- threadWrite may work.</span><span>
</span><span id="line-448"></span><span class="hs-identifier">hwndReadNonBlocking</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-449"></span><span>                    </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">Int</span><span class="hs-special">)</span><span>
</span><span id="line-450"></span><span class="hs-identifier">hwndReadNonBlocking</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-451"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">val</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">withOverlapped</span><span> </span><span class="hs-string">&quot;hwndReadNonBlocking&quot;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span>
</span><span id="line-452"></span><span>                              </span><span class="hs-special">(</span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">ptr</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">completionCB</span><span>
</span><span id="line-453"></span><span>       </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ioValue</span><span> </span><span class="hs-identifier">val</span><span>
</span><span id="line-454"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-455"></span><span>    </span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">inputBuf</span><span> </span><span class="hs-identifier">lpOverlapped</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-456"></span><span>      </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: hwndReadNonBlocking&quot;</span><span>
</span><span id="line-457"></span><span>      </span><span class="hs-comment">-- See Note [ReadFile/WriteFile].</span><span>
</span><span id="line-458"></span><span>      </span><span class="hs-identifier">ret</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_ReadFile</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">inputBuf</span><span class="hs-special">)</span><span>
</span><span id="line-459"></span><span>                        </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">bytes</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">nullPtr</span><span> </span><span class="hs-identifier">lpOverlapped</span><span>
</span><span id="line-460"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Mgr.CbNone</span><span> </span><span class="hs-identifier">ret</span><span>
</span><span id="line-461"></span><span>
</span><span id="line-462"></span><span>    </span><span class="hs-identifier">completionCB</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-463"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span>       </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-operator">$!</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-464"></span><span class="hs-pragma">{-# LINE 464 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-464"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">38</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-465"></span><span class="hs-pragma">{-# LINE 465 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-465"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1073741807</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-466"></span><span class="hs-pragma">{-# LINE 466 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-466"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">109</span><span>   </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-467"></span><span class="hs-pragma">{-# LINE 467 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-467"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1073741493</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-468"></span><span class="hs-pragma">{-# LINE 468 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-468"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">259</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-469"></span><span class="hs-pragma">{-# LINE 469 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-469"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">234</span><span>     </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-operator">$!</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-470"></span><span class="hs-pragma">{-# LINE 470 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-470"></span><span>      </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>                           </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioFailedAny</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-471"></span><span>
</span><span id="line-472"></span><span class="hs-identifier">hwndWrite</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-473"></span><span class="hs-identifier">hwndWrite</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-474"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">Mgr.withException</span><span> </span><span class="hs-string">&quot;hwndWrite&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-475"></span><span>          </span><span class="hs-identifier">withOverlapped</span><span> </span><span class="hs-string">&quot;hwndWrite&quot;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">ptr</span><span class="hs-special">)</span><span>
</span><span id="line-476"></span><span>                         </span><span class="hs-identifier">completionCB</span><span>
</span><span id="line-477"></span><span>       </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-478"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-479"></span><span>    </span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">outBuf</span><span> </span><span class="hs-identifier">lpOverlapped</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-480"></span><span>      </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: hwndWrite&quot;</span><span>
</span><span id="line-481"></span><span>      </span><span class="hs-comment">-- See Note [ReadFile/WriteFile].</span><span>
</span><span id="line-482"></span><span>      </span><span class="hs-identifier">ret</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_WriteFile</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">outBuf</span><span class="hs-special">)</span><span>
</span><span id="line-483"></span><span>                         </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">bytes</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">nullPtr</span><span> </span><span class="hs-identifier">lpOverlapped</span><span>
</span><span id="line-484"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Mgr.CbNone</span><span> </span><span class="hs-identifier">ret</span><span>
</span><span id="line-485"></span><span>
</span><span id="line-486"></span><span>    </span><span class="hs-identifier">completionCB</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-487"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span>  </span><span class="hs-glyph">=</span><span>   </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-488"></span><span class="hs-pragma">{-# LINE 488 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-488"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">38</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-489"></span><span class="hs-pragma">{-# LINE 489 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-489"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>                        </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioFailed</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-490"></span><span>
</span><span id="line-491"></span><span class="hs-identifier">hwndWriteNonBlocking</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-492"></span><span class="hs-identifier">hwndWriteNonBlocking</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-493"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">val</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">withOverlapped</span><span> </span><span class="hs-string">&quot;hwndReadNonBlocking&quot;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span>
</span><span id="line-494"></span><span>                             </span><span class="hs-special">(</span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">ptr</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">completionCB</span><span>
</span><span id="line-495"></span><span>       </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ioValue</span><span> </span><span class="hs-identifier">val</span><span>
</span><span id="line-496"></span><span>  </span><span class="hs-keyword">where</span><span>
</span><span id="line-497"></span><span>    </span><span class="hs-identifier">startCB</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Mgr.CbResult</span><span> </span><span class="hs-identifier">a1</span><span class="hs-special">)</span><span>
</span><span id="line-498"></span><span>    </span><span class="hs-identifier">startCB</span><span> </span><span class="hs-identifier">outBuf</span><span> </span><span class="hs-identifier">lpOverlapped</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-499"></span><span>      </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: hwndWriteNonBlocking&quot;</span><span>
</span><span id="line-500"></span><span>      </span><span class="hs-comment">-- See Note [ReadFile/WriteFile].</span><span>
</span><span id="line-501"></span><span>      </span><span class="hs-identifier">ret</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_WriteFile</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">outBuf</span><span class="hs-special">)</span><span>
</span><span id="line-502"></span><span>                         </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">bytes</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">nullPtr</span><span> </span><span class="hs-identifier">lpOverlapped</span><span>
</span><span id="line-503"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">Mgr.CbNone</span><span> </span><span class="hs-identifier">ret</span><span>
</span><span id="line-504"></span><span>
</span><span id="line-505"></span><span>    </span><span class="hs-identifier">completionCB</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-506"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">0</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-507"></span><span class="hs-pragma">{-# LINE 507 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-507"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">38</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioSuccess</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">dwBytes</span><span>
</span><span id="line-508"></span><span class="hs-pragma">{-# LINE 508 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-508"></span><span>        </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>                        </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Mgr.ioFailed</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-509"></span><span>
</span><span id="line-510"></span><span class="hs-comment">-- Note [ReadFile/WriteFile]</span><span>
</span><span id="line-511"></span><span class="hs-comment">-- The results of these functions are somewhat different when working in an</span><span>
</span><span id="line-512"></span><span class="hs-comment">-- asynchronous manner. The returning bool has two meaning.</span><span>
</span><span id="line-513"></span><span class="hs-comment">--</span><span>
</span><span id="line-514"></span><span class="hs-comment">-- True: The operation is done and was completed synchronously.  This is</span><span>
</span><span id="line-515"></span><span class="hs-comment">--       possible because of the optimization flags we enable.  In this case</span><span>
</span><span id="line-516"></span><span class="hs-comment">--       there won't be a completion event for this call and so we shouldn't</span><span>
</span><span id="line-517"></span><span class="hs-comment">--       queue one up. If we do this request will never terminate.  It's also</span><span>
</span><span id="line-518"></span><span class="hs-comment">--       safe to free the OVERLAPPED structure immediately.</span><span>
</span><span id="line-519"></span><span class="hs-comment">--</span><span>
</span><span id="line-520"></span><span class="hs-comment">-- False: Only indicates that the operation was not completed synchronously, a</span><span>
</span><span id="line-521"></span><span class="hs-comment">--        call to GetLastError () is needed to find out the actual status. If</span><span>
</span><span id="line-522"></span><span class="hs-comment">--        the result is ERROR_IO_PENDING then the operation has been queued on</span><span>
</span><span id="line-523"></span><span class="hs-comment">--        the completion port and we should proceed asynchronously.  Any other</span><span>
</span><span id="line-524"></span><span class="hs-comment">--        state is usually an indication that the call failed.</span><span>
</span><span id="line-525"></span><span class="hs-comment">--</span><span>
</span><span id="line-526"></span><span class="hs-comment">-- NB. reading an EOF will result in ERROR_HANDLE_EOF or STATUS_END_OF_FILE</span><span>
</span><span id="line-527"></span><span class="hs-comment">-- during the checking of the completion results.  We need to check for these</span><span>
</span><span id="line-528"></span><span class="hs-comment">-- so we don't incorrectly fail.</span><span>
</span><span id="line-529"></span><span>
</span><span id="line-530"></span><span>
</span><span id="line-531"></span><span class="hs-identifier">consoleWrite</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-532"></span><span class="hs-identifier">consoleWrite</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">_offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-533"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">res</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-534"></span><span>      </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.consoleWrite&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-535"></span><span>           </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: consoleWrite&quot;</span><span>
</span><span id="line-536"></span><span>           </span><span class="hs-identifier">withGhcInternalToUTF16</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">bytes</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-special">(</span><span class="hs-identifier">w_ptr</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">w_len</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-537"></span><span>              </span><span class="hs-identifier">success</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_write_console</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">w_ptr</span><span>
</span><span id="line-538"></span><span>                                         </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">w_len</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">res</span><span> </span><span class="hs-identifier">nullPtr</span><span>
</span><span id="line-539"></span><span>              </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">not</span><span> </span><span class="hs-identifier">success</span><span>
</span><span id="line-540"></span><span>                 </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">False</span><span>
</span><span id="line-541"></span><span>                 </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">val</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-542"></span><span>                         </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">val</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">w_len</span><span>
</span><span id="line-543"></span><span>
</span><span id="line-544"></span><span class="hs-identifier">consoleWriteNonBlocking</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-545"></span><span class="hs-identifier">consoleWriteNonBlocking</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">_offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-546"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">res</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-547"></span><span>      </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.consoleWriteNonBlocking&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-548"></span><span>            </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;:: consoleWriteNonBlocking&quot;</span><span>
</span><span id="line-549"></span><span>            </span><span class="hs-identifier">withGhcInternalToUTF16</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">bytes</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-special">(</span><span class="hs-identifier">w_ptr</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">w_len</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-550"></span><span>              </span><span class="hs-identifier">c_write_console</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">w_len</span><span class="hs-special">)</span><span>
</span><span id="line-551"></span><span>                              </span><span class="hs-identifier">res</span><span> </span><span class="hs-identifier">nullPtr</span><span>
</span><span id="line-552"></span><span>         </span><span class="hs-identifier">val</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-553"></span><span>         </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">val</span><span>
</span><span id="line-554"></span><span>
</span><span id="line-555"></span><span class="hs-identifier">consoleRead</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-556"></span><span class="hs-identifier">consoleRead</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">_offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-557"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">withUTF16ToGhcInternal</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">bytes</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">reqBytes</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-558"></span><span>      </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">res</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-559"></span><span>       </span><span class="hs-identifier">cooked</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">isCooked</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-560"></span><span>       </span><span class="hs-comment">-- Cooked input must be handled differently when the STD handles are</span><span>
</span><span id="line-561"></span><span>       </span><span class="hs-comment">-- attached to a real console handle.  For File based handles we can't do</span><span>
</span><span id="line-562"></span><span>       </span><span class="hs-comment">-- proper cooked inputs, but since the actions are async you would get</span><span>
</span><span id="line-563"></span><span>       </span><span class="hs-comment">-- results as soon as available.</span><span>
</span><span id="line-564"></span><span>       </span><span class="hs-comment">--</span><span>
</span><span id="line-565"></span><span>       </span><span class="hs-comment">-- For console handles We have to use a lower level API then ReadConsole,</span><span>
</span><span id="line-566"></span><span>       </span><span class="hs-comment">-- namely we must use ReadConsoleInput which requires us to process</span><span>
</span><span id="line-567"></span><span>       </span><span class="hs-comment">-- all console message manually.</span><span>
</span><span id="line-568"></span><span>       </span><span class="hs-comment">--</span><span>
</span><span id="line-569"></span><span>       </span><span class="hs-comment">-- Do note that MSYS2 shells such as bash don't attach to a real handle,</span><span>
</span><span id="line-570"></span><span>       </span><span class="hs-comment">-- and instead have by default a pipe/file based std handles.  Which</span><span>
</span><span id="line-571"></span><span>       </span><span class="hs-comment">-- means the cooked behaviour is best when used in a native Windows</span><span>
</span><span id="line-572"></span><span>       </span><span class="hs-comment">-- terminal such as cmd, powershell or ConEmu.</span><span>
</span><span id="line-573"></span><span>       </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">cooked</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-574"></span><span>        </span><span class="hs-identifier">False</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-575"></span><span>          </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;consoleRead :: un-cooked I/O read.&quot;</span><span>
</span><span id="line-576"></span><span>          </span><span class="hs-comment">-- eotControl allows us to handle control characters like EOL</span><span>
</span><span id="line-577"></span><span>          </span><span class="hs-comment">-- without needing a newline, which would sort of defeat the point</span><span>
</span><span id="line-578"></span><span>          </span><span class="hs-comment">-- of an EOL.</span><span>
</span><span id="line-579"></span><span>          </span><span class="hs-identifier">res_code</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">with</span><span> </span><span class="hs-identifier">eotControl</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">p_eotControl</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-580"></span><span>                </span><span class="hs-identifier">c_read_console</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">reqBytes</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-581"></span><span>                               </span><span class="hs-identifier">p_eotControl</span><span>
</span><span id="line-582"></span><span>
</span><span id="line-583"></span><span>          </span><span class="hs-comment">-- Restore a quirk of the POSIX read call, which only returns a fail</span><span>
</span><span id="line-584"></span><span>          </span><span class="hs-comment">-- when the handle is invalid, e.g. closed or not a handle.  It how-</span><span>
</span><span id="line-585"></span><span>          </span><span class="hs-comment">-- ever returns 0 when the handle is valid but unreadable, such as</span><span>
</span><span id="line-586"></span><span>          </span><span class="hs-comment">-- passing a handle with no GENERIC_READ permission, like /dev/null</span><span>
</span><span id="line-587"></span><span>          </span><span class="hs-identifier">err</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getLastError</span><span>
</span><span id="line-588"></span><span>          </span><span class="hs-identifier">when</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">not</span><span> </span><span class="hs-identifier">res_code</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-589"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-590"></span><span>             </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">1</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-591"></span><span class="hs-pragma">{-# LINE 591 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-591"></span><span>               </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">failWith</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.consoleRead&quot;</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-592"></span><span>          </span><span class="hs-identifier">b_read</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-593"></span><span>          </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">b_read</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-594"></span><span>              </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">b_read</span><span>
</span><span id="line-595"></span><span>              </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">w_first</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peekElemOff</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-596"></span><span>                      </span><span class="hs-keyword">case</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-597"></span><span>                        </span><span class="hs-comment">-- Handle Ctrl+Z which is the actual EOL sequence on</span><span>
</span><span id="line-598"></span><span>                        </span><span class="hs-comment">-- windows, but also handle Ctrl+D which is what the</span><span>
</span><span id="line-599"></span><span>                        </span><span class="hs-comment">-- ASCII standard defines as EOL.</span><span>
</span><span id="line-600"></span><span>                        </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">w_first</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">acCtrlD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-601"></span><span>                          </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">w_first</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">acCtrlZ</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-602"></span><span>                          </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>                       </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">b_read</span><span>
</span><span id="line-603"></span><span>        </span><span class="hs-identifier">True</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-604"></span><span>          </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;consoleRead :: cooked I/O read.&quot;</span><span>
</span><span id="line-605"></span><span>          </span><span class="hs-comment">-- Input is cooked, don't wait till a line return and consume all</span><span>
</span><span id="line-606"></span><span>          </span><span class="hs-comment">-- characters as they are.  Technically this function can handle any</span><span>
</span><span id="line-607"></span><span>          </span><span class="hs-comment">-- console event.  Including mouse, window and virtual key events</span><span>
</span><span id="line-608"></span><span>          </span><span class="hs-comment">-- but for now I'm only interested in key presses.</span><span>
</span><span id="line-609"></span><span>          </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">entries</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">reqBytes</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">div</span><span class="hs-special">`</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-number">20</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-610"></span><span class="hs-pragma">{-# LINE 610 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-610"></span><span>          </span><span class="hs-identifier">allocaBytes</span><span> </span><span class="hs-identifier">entries</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-611"></span><span>            </span><span class="hs-identifier">readEvent</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">entries</span><span> </span><span class="hs-identifier">res</span><span> </span><span class="hs-identifier">w_ptr</span><span>
</span><span id="line-612"></span><span>
</span><span id="line-613"></span><span>    </span><span class="hs-keyword">where</span><span> </span><span class="hs-identifier">readEvent</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">entries</span><span> </span><span class="hs-identifier">res</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-614"></span><span>            </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.consoleRead&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-615"></span><span>              </span><span class="hs-identifier">c_read_console_input</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">p_inputs</span><span>
</span><span id="line-616"></span><span>                                   </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">entries</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-617"></span><span>
</span><span id="line-618"></span><span>            </span><span class="hs-identifier">b_read</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-619"></span><span>            </span><span class="hs-identifier">read</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-identifier">b_read</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-identifier">p_inputs</span><span>
</span><span id="line-620"></span><span>            </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">read</span><span> </span><span class="hs-operator">&gt;</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-621"></span><span>               </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">read</span><span>
</span><span id="line-622"></span><span>               </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">readEvent</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">entries</span><span> </span><span class="hs-identifier">res</span><span> </span><span class="hs-identifier">w_ptr</span><span>
</span><span id="line-623"></span><span>
</span><span id="line-624"></span><span>          </span><span class="hs-comment">-- Dereference and read console input records.  We only read the bare</span><span>
</span><span id="line-625"></span><span>          </span><span class="hs-comment">-- minimum required to know which key/sequences were pressed.  To do</span><span>
</span><span id="line-626"></span><span>          </span><span class="hs-comment">-- this and prevent having to fully port the PINPUT_RECORD structure</span><span>
</span><span id="line-627"></span><span>          </span><span class="hs-comment">-- in Haskell we use some GCC builtins to find the correct offsets.</span><span>
</span><span id="line-628"></span><span>          </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word16</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">PINPUT_RECORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-629"></span><span>          </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-string">&quot;cobble: done.&quot;</span><span>
</span><span id="line-630"></span><span>                            </span><span class="hs-identifier">return</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-631"></span><span>          </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-identifier">n</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-632"></span><span>            </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">eventType</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">WORD</span><span>
</span><span id="line-633"></span><span>               </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: Length=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">n</span><span>
</span><span id="line-634"></span><span>               </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: Type=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">eventType</span><span>
</span><span id="line-635"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">ni_offset</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="hs-number">20</span><span class="hs-special">)</span><span>
</span><span id="line-636"></span><span class="hs-pragma">{-# LINE 636 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-636"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">event</span><span>          </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">4</span><span>
</span><span id="line-637"></span><span class="hs-pragma">{-# LINE 637 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-637"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">char_offset</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">event</span><span> </span><span class="hs-operator">+</span><span> </span><span class="hs-number">10</span><span>
</span><span id="line-638"></span><span class="hs-pragma">{-# LINE 638 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-638"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">btnDown_offset</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">event</span><span> </span><span class="hs-operator">+</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-639"></span><span class="hs-pragma">{-# LINE 639 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-639"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">repeat_offset</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">event</span><span> </span><span class="hs-operator">+</span><span> </span><span class="hs-number">4</span><span>
</span><span id="line-640"></span><span class="hs-pragma">{-# LINE 640 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-640"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">n'</span><span>             </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">n</span><span> </span><span class="hs-glyph">-</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-641"></span><span>               </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">p_inputs'</span><span>      </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">plusPtr</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">ni_offset</span><span>
</span><span id="line-642"></span><span>               </span><span class="hs-identifier">btnDown</span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">btnDown_offset</span><span>
</span><span id="line-643"></span><span>               </span><span class="hs-identifier">repeated</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">repeat_offset</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">WORD</span><span class="hs-special">)</span><span>
</span><span id="line-644"></span><span>               </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: BtnDown=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">btnDown</span><span>
</span><span id="line-645"></span><span>               </span><span class="hs-comment">-- Handle the key only on button down and not on button up.</span><span>
</span><span id="line-646"></span><span>               </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">eventType</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">1</span><span> </span><span class="hs-operator">&amp;&amp;</span><span> </span><span class="hs-identifier">btnDown</span><span>
</span><span id="line-647"></span><span class="hs-pragma">{-# LINE 647 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-647"></span><span>                  </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: read-char.&quot;</span><span>
</span><span id="line-648"></span><span>                          </span><span class="hs-identifier">char</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">p_inputs</span><span> </span><span class="hs-identifier">char_offset</span><span>
</span><span id="line-649"></span><span>                          </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">w_ptr'</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">plusPtr</span><span class="hs-special">`</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-650"></span><span>                          </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: offset - &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">char_offset</span><span>
</span><span id="line-651"></span><span>                          </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: show &gt; &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">char</span><span>
</span><span id="line-652"></span><span>                          </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: repeat: &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">repeated</span><span>
</span><span id="line-653"></span><span>                          </span><span class="hs-identifier">pokeArray</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">replicate</span><span> </span><span class="hs-identifier">repeated</span><span> </span><span class="hs-identifier">char</span><span>
</span><span id="line-654"></span><span>                          </span><span class="hs-special">(</span><span class="hs-operator">+</span><span class="hs-number">1</span><span class="hs-special">)</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-identifier">n'</span><span> </span><span class="hs-identifier">w_ptr'</span><span> </span><span class="hs-identifier">p_inputs'</span><span>
</span><span id="line-655"></span><span>                  </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;cobble: skip event.&quot;</span><span>
</span><span id="line-656"></span><span>                          </span><span class="hs-identifier">cobble</span><span> </span><span class="hs-identifier">n'</span><span> </span><span class="hs-identifier">w_ptr</span><span> </span><span class="hs-identifier">p_inputs'</span><span>
</span><span id="line-657"></span><span>
</span><span id="line-658"></span><span>
</span><span id="line-659"></span><span class="hs-identifier">consoleReadNonBlocking</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word8</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-660"></span><span>                       </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">Int</span><span class="hs-special">)</span><span>
</span><span id="line-661"></span><span class="hs-identifier">consoleReadNonBlocking</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-662"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">consoleRead</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-663"></span><span>
</span><span id="line-664"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-665"></span><span class="hs-comment">-- Operations on file handles</span><span>
</span><span id="line-666"></span><span>
</span><span id="line-667"></span><span class="hs-identifier">handle_ready</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Int</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-668"></span><span class="hs-identifier">handle_ready</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">write</span><span> </span><span class="hs-identifier">msecs</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-669"></span><span>  </span><span class="hs-identifier">r</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">throwErrnoIfMinus1Retry</span><span> </span><span class="hs-string">&quot;GHC.IO.Windows.Handle.handle_ready&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-670"></span><span>          </span><span class="hs-identifier">c_handle_ready</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">write</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">msecs</span><span class="hs-special">)</span><span>
</span><span id="line-671"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toEnum</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">r</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-672"></span><span>
</span><span id="line-673"></span><span class="hs-identifier">handle_is_console</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-674"></span><span class="hs-identifier">handle_is_console</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">c_is_console</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">toHANDLE</span><span>
</span><span id="line-675"></span><span>
</span><span id="line-676"></span><span class="hs-identifier">handle_close</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-677"></span><span class="hs-identifier">handle_close</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">release</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-678"></span><span>                    </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;handle_close&quot;</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">c_close_handle</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">h</span><span class="hs-special">)</span><span>
</span><span id="line-679"></span><span>
</span><span id="line-680"></span><span class="hs-identifier">handle_dev_type</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">IODeviceType</span><span>
</span><span id="line-681"></span><span class="hs-identifier">handle_dev_type</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_handle_type</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-682"></span><span>                          </span><span class="hs-identifier">return</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-683"></span><span>                                     </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">3</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Stream</span><span>
</span><span id="line-684"></span><span>                                       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">5</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">RawDevice</span><span>
</span><span id="line-685"></span><span>                                       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">RegularFile</span><span>
</span><span id="line-686"></span><span>
</span><span id="line-687"></span><span class="hs-identifier">handle_is_seekable</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-688"></span><span class="hs-identifier">handle_is_seekable</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-689"></span><span>  </span><span class="hs-identifier">t</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">handle_dev_type</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-690"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">t</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">RegularFile</span><span> </span><span class="hs-operator">||</span><span> </span><span class="hs-identifier">t</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">RawDevice</span><span class="hs-special">)</span><span>
</span><span id="line-691"></span><span>
</span><span id="line-692"></span><span class="hs-identifier">handle_seek</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">SeekMode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Integer</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-693"></span><span class="hs-identifier">handle_seek</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">mode</span><span> </span><span class="hs-identifier">off</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-694"></span><span>  </span><span class="hs-identifier">with</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">off_rel</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-695"></span><span>    </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_seek&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-696"></span><span>        </span><span class="hs-identifier">c_set_file_pointer</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">off</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-identifier">off_rel</span><span>
</span><span id="line-697"></span><span>    </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">off_rel</span><span>
</span><span id="line-698"></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-699"></span><span>    </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-700"></span><span>    </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">mode</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-701"></span><span>                   </span><span class="hs-identifier">AbsoluteSeek</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-702"></span><span class="hs-pragma">{-# LINE 702 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-702"></span><span>                   </span><span class="hs-identifier">RelativeSeek</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-703"></span><span class="hs-pragma">{-# LINE 703 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-703"></span><span>                   </span><span class="hs-identifier">SeekFromEnd</span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">2</span><span>
</span><span id="line-704"></span><span class="hs-pragma">{-# LINE 704 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-704"></span><span>
</span><span id="line-705"></span><span class="hs-identifier">handle_tell</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-706"></span><span class="hs-identifier">handle_tell</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-707"></span><span>   </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span>
</span><span id="line-708"></span><span>      </span><span class="hs-special">(</span><span class="hs-identifier">throwErrnoIfMinus1Retry</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_tell&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-709"></span><span>          </span><span class="hs-identifier">c_get_file_pointer</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-710"></span><span>
</span><span id="line-711"></span><span class="hs-identifier">handle_set_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Integer</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-712"></span><span class="hs-identifier">handle_set_size</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">size</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-713"></span><span>  </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_set_size&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-714"></span><span>      </span><span class="hs-identifier">c_set_file_size</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">size</span><span class="hs-special">)</span><span>
</span><span id="line-715"></span><span>
</span><span id="line-716"></span><span class="hs-identifier">handle_get_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-717"></span><span class="hs-identifier">handle_get_size</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-718"></span><span>   </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span>
</span><span id="line-719"></span><span>      </span><span class="hs-special">(</span><span class="hs-identifier">throwErrnoIfMinus1Retry</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_set_size&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-720"></span><span>          </span><span class="hs-identifier">c_get_file_size</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-721"></span><span>
</span><span id="line-722"></span><span class="hs-identifier">handle_set_echo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-723"></span><span class="hs-identifier">handle_set_echo</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">value</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-724"></span><span>  </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_set_echo&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-725"></span><span>      </span><span class="hs-identifier">c_set_console_echo</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">value</span><span>
</span><span id="line-726"></span><span>
</span><span id="line-727"></span><span class="hs-identifier">handle_get_echo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-728"></span><span class="hs-identifier">handle_get_echo</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">c_get_console_echo</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">toHANDLE</span><span>
</span><span id="line-729"></span><span>
</span><span id="line-730"></span><span class="hs-identifier">handle_duplicate</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-731"></span><span class="hs-identifier">handle_duplicate</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-732"></span><span>  </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_duplicate&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-733"></span><span>      </span><span class="hs-identifier">c_duplicate_handle</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-734"></span><span>  </span><span class="hs-identifier">fromHANDLE</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-735"></span><span>
</span><span id="line-736"></span><span class="hs-identifier">console_set_buffering</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">ConsoleHandle</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-737"></span><span class="hs-identifier">console_set_buffering</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">value</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">setCooked</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">value</span><span> </span><span class="hs-operator">&gt;&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-738"></span><span>
</span><span id="line-739"></span><span class="hs-identifier">handle_set_buffering</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-740"></span><span class="hs-identifier">handle_set_buffering</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">value</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-741"></span><span>  </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_set_buffering&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-742"></span><span>      </span><span class="hs-identifier">c_set_console_buffering</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">value</span><span>
</span><span id="line-743"></span><span>
</span><span id="line-744"></span><span class="hs-identifier">handle_console_seek</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">SeekMode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Integer</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-745"></span><span class="hs-identifier">handle_console_seek</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">mode</span><span> </span><span class="hs-identifier">off</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-746"></span><span>  </span><span class="hs-identifier">with</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">loc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-747"></span><span>    </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_console_seek&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-748"></span><span>      </span><span class="hs-identifier">c_set_console_pointer</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">off</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-identifier">loc_ptr</span><span>
</span><span id="line-749"></span><span>    </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">&lt;$&gt;</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">loc_ptr</span><span>
</span><span id="line-750"></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-751"></span><span>    </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-752"></span><span>    </span><span class="hs-identifier">seektype</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">mode</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-753"></span><span>                 </span><span class="hs-identifier">AbsoluteSeek</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-754"></span><span class="hs-pragma">{-# LINE 754 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-754"></span><span>                 </span><span class="hs-identifier">RelativeSeek</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">1</span><span>
</span><span id="line-755"></span><span class="hs-pragma">{-# LINE 755 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-755"></span><span>                 </span><span class="hs-identifier">SeekFromEnd</span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">2</span><span>
</span><span id="line-756"></span><span class="hs-pragma">{-# LINE 756 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-756"></span><span>
</span><span id="line-757"></span><span class="hs-identifier">handle_console_tell</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-758"></span><span class="hs-identifier">handle_console_tell</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-759"></span><span>   </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span>
</span><span id="line-760"></span><span>      </span><span class="hs-special">(</span><span class="hs-identifier">throwErrnoIfMinus1Retry</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_console_tell&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-761"></span><span>          </span><span class="hs-identifier">c_get_console_pointer</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-762"></span><span>
</span><span id="line-763"></span><span class="hs-identifier">handle_set_console_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Integer</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-764"></span><span class="hs-identifier">handle_set_console_size</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-identifier">size</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-765"></span><span>  </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_set_console_size&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-766"></span><span>      </span><span class="hs-identifier">c_set_console_buffer_size</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">size</span><span class="hs-special">)</span><span>
</span><span id="line-767"></span><span>
</span><span id="line-768"></span><span class="hs-identifier">handle_get_console_size</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Integer</span><span>
</span><span id="line-769"></span><span class="hs-identifier">handle_get_console_size</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-770"></span><span>   </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span>
</span><span id="line-771"></span><span>      </span><span class="hs-special">(</span><span class="hs-identifier">throwErrnoIfMinus1Retry</span><span> </span><span class="hs-string">&quot;GHC.IO.Handle.handle_get_console_size&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-772"></span><span>          </span><span class="hs-identifier">c_get_console_buffer_size</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">hwnd</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-773"></span><span>
</span><span id="line-774"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-775"></span><span class="hs-comment">-- opening files</span><span>
</span><span id="line-776"></span><span>
</span><span id="line-777"></span><span class="hs-comment">-- | Describes if and which temp file flags to use.</span><span>
</span><span id="line-778"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">TempFileOptions</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">NoTemp</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">TempNonExcl</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">TempExcl</span><span> </span><span class="hs-keyword">deriving</span><span> </span><span class="hs-identifier">Eq</span><span>
</span><span id="line-779"></span><span>
</span><span id="line-780"></span><span class="hs-comment">-- | Open a file and make an 'NativeHandle' for it.  Truncates the file to zero</span><span>
</span><span id="line-781"></span><span class="hs-comment">-- size when the `IOMode` is `WriteMode`.</span><span>
</span><span id="line-782"></span><span class="hs-identifier">openFile</span><span>
</span><span id="line-783"></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FilePath</span><span> </span><span class="hs-comment">-- ^ file to open</span><span>
</span><span id="line-784"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IOMode</span><span>   </span><span class="hs-comment">-- ^ mode in which to open the file</span><span>
</span><span id="line-785"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span>     </span><span class="hs-comment">-- ^ open the file in non-blocking mode?</span><span>
</span><span id="line-786"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODeviceType</span><span class="hs-special">)</span><span>
</span><span id="line-787"></span><span class="hs-identifier">openFile</span><span> </span><span class="hs-identifier">filepath</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-identifier">non_blocking</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">openFile'</span><span> </span><span class="hs-identifier">filepath</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-identifier">non_blocking</span><span> </span><span class="hs-identifier">NoTemp</span><span>
</span><span id="line-788"></span><span>
</span><span id="line-789"></span><span class="hs-comment">-- | Open a file as a temporary file and make an 'NativeHandle' for it.</span><span>
</span><span id="line-790"></span><span class="hs-comment">-- Truncates the file to zero size when the `IOMode` is `WriteMode`.</span><span>
</span><span id="line-791"></span><span class="hs-identifier">openFileAsTemp</span><span>
</span><span id="line-792"></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FilePath</span><span> </span><span class="hs-comment">-- ^ file to open</span><span>
</span><span id="line-793"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span>     </span><span class="hs-comment">-- ^ open the file in non-blocking mode?</span><span>
</span><span id="line-794"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span>     </span><span class="hs-comment">-- ^ Exclusive mode</span><span>
</span><span id="line-795"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODeviceType</span><span class="hs-special">)</span><span>
</span><span id="line-796"></span><span class="hs-identifier">openFileAsTemp</span><span> </span><span class="hs-identifier">filepath</span><span> </span><span class="hs-identifier">non_blocking</span><span> </span><span class="hs-identifier">excl</span><span>
</span><span id="line-797"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">openFile'</span><span> </span><span class="hs-identifier">filepath</span><span> </span><span class="hs-identifier">ReadWriteMode</span><span> </span><span class="hs-identifier">non_blocking</span><span> </span><span class="hs-special">(</span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">excl</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">TempExcl</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">TempNonExcl</span><span class="hs-special">)</span><span>
</span><span id="line-798"></span><span>
</span><span id="line-799"></span><span class="hs-comment">-- | Open a file and make an 'NativeHandle' for it.  Truncates the file to zero</span><span>
</span><span id="line-800"></span><span class="hs-comment">-- size when the `IOMode` is `WriteMode`.</span><span>
</span><span id="line-801"></span><span class="hs-identifier">openFile'</span><span>
</span><span id="line-802"></span><span>  </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FilePath</span><span> </span><span class="hs-comment">-- ^ file to open</span><span>
</span><span id="line-803"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IOMode</span><span>   </span><span class="hs-comment">-- ^ mode in which to open the file</span><span>
</span><span id="line-804"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Bool</span><span>     </span><span class="hs-comment">-- ^ open the file in non-blocking mode?</span><span>
</span><span id="line-805"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">TempFileOptions</span><span>
</span><span id="line-806"></span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Io</span><span> </span><span class="hs-identifier">NativeHandle</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">IODeviceType</span><span class="hs-special">)</span><span>
</span><span id="line-807"></span><span class="hs-identifier">openFile'</span><span> </span><span class="hs-identifier">filepath</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-identifier">non_blocking</span><span> </span><span class="hs-identifier">tmp_opts</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-808"></span><span>   </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">devicepath</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getDevicePath</span><span> </span><span class="hs-identifier">filepath</span><span>
</span><span id="line-809"></span><span>      </span><span class="hs-identifier">h</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">createFile</span><span> </span><span class="hs-identifier">devicepath</span><span>
</span><span id="line-810"></span><span>      </span><span class="hs-comment">-- Attach the handle to the I/O manager's CompletionPort.  This allows the</span><span>
</span><span id="line-811"></span><span>      </span><span class="hs-comment">-- I/O manager to service requests for this Handle.</span><span>
</span><span id="line-812"></span><span>      </span><span class="hs-identifier">Mgr.associateHandle'</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-813"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromHANDLE</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-814"></span><span>      </span><span class="hs-identifier">_type</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">devType</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-815"></span><span>
</span><span id="line-816"></span><span>      </span><span class="hs-comment">-- Use the rts to enforce any file locking we may need.</span><span>
</span><span id="line-817"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">write_lock</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-identifier">ReadMode</span><span>
</span><span id="line-818"></span><span>
</span><span id="line-819"></span><span>      </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-820"></span><span>        </span><span class="hs-comment">-- Regular files need to be locked.</span><span>
</span><span id="line-821"></span><span>        </span><span class="hs-comment">-- See also Note [RTS File locking]</span><span>
</span><span id="line-822"></span><span>        </span><span class="hs-identifier">RegularFile</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-823"></span><span>          </span><span class="hs-identifier">optimizeFileAccess</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-comment">-- Set a few optimization flags on file handles.</span><span>
</span><span id="line-824"></span><span>          </span><span class="hs-special">(</span><span class="hs-identifier">unique_dev</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">unique_ino</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getUniqueFileInfo</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-825"></span><span>          </span><span class="hs-identifier">r</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">lockFile</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ptrToWordPtr</span><span> </span><span class="hs-identifier">h</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">unique_dev</span><span> </span><span class="hs-identifier">unique_ino</span><span>
</span><span id="line-826"></span><span>                        </span><span class="hs-special">(</span><span class="hs-identifier">fromBool</span><span> </span><span class="hs-identifier">write_lock</span><span class="hs-special">)</span><span>
</span><span id="line-827"></span><span>          </span><span class="hs-identifier">when</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">r</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-glyph">-</span><span class="hs-number">1</span><span class="hs-special">)</span><span>  </span><span class="hs-operator">$</span><span>
</span><span id="line-828"></span><span>               </span><span class="hs-identifier">ioException</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">IOError</span><span> </span><span class="hs-identifier">Nothing</span><span> </span><span class="hs-identifier">ResourceBusy</span><span> </span><span class="hs-string">&quot;openFile&quot;</span><span>
</span><span id="line-829"></span><span>                                  </span><span class="hs-string">&quot;file is locked&quot;</span><span> </span><span class="hs-identifier">Nothing</span><span> </span><span class="hs-identifier">Nothing</span><span class="hs-special">)</span><span>
</span><span id="line-830"></span><span>
</span><span id="line-831"></span><span>        </span><span class="hs-comment">-- I don't see a reason for blocking directories.  So unlike the FD</span><span>
</span><span id="line-832"></span><span>        </span><span class="hs-comment">-- implementation I'll allow it.</span><span>
</span><span id="line-833"></span><span>        </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-834"></span><span>
</span><span id="line-835"></span><span>      </span><span class="hs-comment">-- We want to truncate() if this is an open in WriteMode, but only</span><span>
</span><span id="line-836"></span><span>      </span><span class="hs-comment">-- if the target is a RegularFile.  but TRUNCATE_EXISTING would fail if</span><span>
</span><span id="line-837"></span><span>      </span><span class="hs-comment">-- the file didn't exit.  So just set the size afterwards.</span><span>
</span><span id="line-838"></span><span>      </span><span class="hs-identifier">when</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">iomode</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">WriteMode</span><span> </span><span class="hs-operator">&amp;&amp;</span><span> </span><span class="hs-identifier">_type</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">RegularFile</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-839"></span><span>        </span><span class="hs-identifier">setSize</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-840"></span><span>
</span><span id="line-841"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">hwnd</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">_type</span><span class="hs-special">)</span><span>
</span><span id="line-842"></span><span>        </span><span class="hs-keyword">where</span><span>
</span><span id="line-843"></span><span>          </span><span class="hs-identifier">flagIf</span><span> </span><span class="hs-identifier">p</span><span> </span><span class="hs-identifier">f2</span><span>
</span><span id="line-844"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">p</span><span>         </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">f2</span><span>
</span><span id="line-845"></span><span>            </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-846"></span><span>          </span><span class="hs-comment">-- We have to use in-process locking (e.g. use the locking mechanism</span><span>
</span><span id="line-847"></span><span>          </span><span class="hs-comment">-- in the rts) so we're consistent with the linux behavior and the</span><span>
</span><span id="line-848"></span><span>          </span><span class="hs-comment">-- rts knows about the lock.  See #4363 for more.</span><span>
</span><span id="line-849"></span><span>          </span><span class="hs-identifier">file_share_mode</span><span> </span><span class="hs-glyph">=</span><span>  </span><span class="hs-number">1</span><span>
</span><span id="line-850"></span><span class="hs-pragma">{-# LINE 850 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-850"></span><span>                         </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">4</span><span>
</span><span id="line-851"></span><span class="hs-pragma">{-# LINE 851 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-851"></span><span>                         </span><span class="hs-comment">-- Don't support shared writing for temp files.</span><span>
</span><span id="line-852"></span><span>                         </span><span class="hs-operator">.|.</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">flagIf</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">tmp_opts</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">NoTemp</span><span class="hs-special">)</span><span>
</span><span id="line-853"></span><span>                                     </span><span class="hs-number">2</span><span class="hs-special">)</span><span>
</span><span id="line-854"></span><span class="hs-pragma">{-# LINE 854 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-854"></span><span>
</span><span id="line-855"></span><span>          </span><span class="hs-identifier">file_access_mode</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-856"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-857"></span><span>              </span><span class="hs-identifier">ReadMode</span><span>      </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">2147483648</span><span>
</span><span id="line-858"></span><span class="hs-pragma">{-# LINE 858 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-858"></span><span>              </span><span class="hs-identifier">WriteMode</span><span>     </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">1073741824</span><span>
</span><span id="line-859"></span><span class="hs-pragma">{-# LINE 859 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-859"></span><span>              </span><span class="hs-identifier">ReadWriteMode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">2147483648</span><span>
</span><span id="line-860"></span><span class="hs-pragma">{-# LINE 860 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-860"></span><span>                            </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">1073741824</span><span>
</span><span id="line-861"></span><span class="hs-pragma">{-# LINE 861 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-861"></span><span>              </span><span class="hs-identifier">AppendMode</span><span>    </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">1073741824</span><span>
</span><span id="line-862"></span><span class="hs-pragma">{-# LINE 862 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-862"></span><span>                            </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">4</span><span>
</span><span id="line-863"></span><span class="hs-pragma">{-# LINE 863 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-863"></span><span>
</span><span id="line-864"></span><span>          </span><span class="hs-identifier">file_open_mode</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-865"></span><span>            </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">iomode</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-866"></span><span>              </span><span class="hs-identifier">ReadMode</span><span>      </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">3</span><span> </span><span class="hs-comment">-- O_RDONLY</span><span>
</span><span id="line-867"></span><span class="hs-pragma">{-# LINE 867 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-867"></span><span>              </span><span class="hs-identifier">WriteMode</span><span>     </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">4</span><span>   </span><span class="hs-comment">-- O_CREAT | O_WRONLY | O_TRUNC</span><span>
</span><span id="line-868"></span><span class="hs-pragma">{-# LINE 868 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-868"></span><span>              </span><span class="hs-identifier">ReadWriteMode</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-869"></span><span>                </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">tmp_opts</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-870"></span><span>                  </span><span class="hs-identifier">NoTemp</span><span>    </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">4</span><span>   </span><span class="hs-comment">-- O_CREAT | O_RDWR</span><span>
</span><span id="line-871"></span><span class="hs-pragma">{-# LINE 871 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-871"></span><span>                  </span><span class="hs-identifier">TempNonExcl</span><span> </span><span class="hs-glyph">-&gt;</span><span>  </span><span class="hs-number">2</span><span> </span><span class="hs-comment">-- O_CREAT | O_RDWR</span><span>
</span><span id="line-872"></span><span class="hs-pragma">{-# LINE 872 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-872"></span><span>                  </span><span class="hs-identifier">TempExcl</span><span>  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">1</span><span>    </span><span class="hs-comment">-- O_CREAT | O_RDWR | O_EXCL</span><span>
</span><span id="line-873"></span><span class="hs-pragma">{-# LINE 873 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-873"></span><span>              </span><span class="hs-identifier">AppendMode</span><span>    </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-number">4</span><span>   </span><span class="hs-comment">-- O_APPEND</span><span>
</span><span id="line-874"></span><span class="hs-pragma">{-# LINE 874 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-874"></span><span>
</span><span id="line-875"></span><span>          </span><span class="hs-identifier">file_create_flags</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-876"></span><span>            </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">non_blocking</span><span>
</span><span id="line-877"></span><span>               </span><span class="hs-comment">-- On Windows, the choice of whether an operation completes</span><span>
</span><span id="line-878"></span><span>               </span><span class="hs-comment">-- asynchronously or not depends on how the Handle was created</span><span>
</span><span id="line-879"></span><span>               </span><span class="hs-comment">-- and not on the operation called.  As in, the behaviour of</span><span>
</span><span id="line-880"></span><span>               </span><span class="hs-comment">-- ReadFile and WriteFile depends on the flags used to open the</span><span>
</span><span id="line-881"></span><span>               </span><span class="hs-comment">-- handle.   For WinIO we always use FILE_FLAG_OVERLAPPED, which</span><span>
</span><span id="line-882"></span><span>               </span><span class="hs-comment">-- means we always issue asynchronous file operation using an</span><span>
</span><span id="line-883"></span><span>               </span><span class="hs-comment">-- OVERLAPPED structure.  All blocking, if required must be done</span><span>
</span><span id="line-884"></span><span>               </span><span class="hs-comment">-- on the Haskell side by using existing mechanisms such as MVar</span><span>
</span><span id="line-885"></span><span>               </span><span class="hs-comment">-- or IOPorts.</span><span>
</span><span id="line-886"></span><span>               </span><span class="hs-keyword">then</span><span> </span><span class="hs-number">1073741824</span><span>
</span><span id="line-887"></span><span class="hs-pragma">{-# LINE 887 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-887"></span><span>                    </span><span class="hs-comment">-- I beleive most haskell programs do sequential scans, so</span><span>
</span><span id="line-888"></span><span>                    </span><span class="hs-comment">-- optimize for the common case.  Though ideally, this would</span><span>
</span><span id="line-889"></span><span>                    </span><span class="hs-comment">-- be parameterized by openFile.  This will absolutely trash</span><span>
</span><span id="line-890"></span><span>                    </span><span class="hs-comment">-- the cache on reverse scans.</span><span>
</span><span id="line-891"></span><span>                    </span><span class="hs-comment">--</span><span>
</span><span id="line-892"></span><span>                    </span><span class="hs-comment">-- TODO: make a parameter to openFile and specify only for</span><span>
</span><span id="line-893"></span><span>                    </span><span class="hs-comment">-- operations we know are sequential.  This parameter should</span><span>
</span><span id="line-894"></span><span>                    </span><span class="hs-comment">-- be usable by madvise too.</span><span>
</span><span id="line-895"></span><span>                    </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">134217728</span><span>
</span><span id="line-896"></span><span class="hs-pragma">{-# LINE 896 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-896"></span><span>                    </span><span class="hs-operator">.|.</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">flagIf</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">tmp_opts</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-identifier">NoTemp</span><span class="hs-special">)</span><span>
</span><span id="line-897"></span><span>                                </span><span class="hs-comment">-- Hold data in cache for as long as possible</span><span>
</span><span id="line-898"></span><span>                                </span><span class="hs-number">256</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-899"></span><span class="hs-pragma">{-# LINE 899 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-899"></span><span>               </span><span class="hs-keyword">else</span><span> </span><span class="hs-number">128</span><span>
</span><span id="line-900"></span><span class="hs-pragma">{-# LINE 900 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-900"></span><span>                    </span><span class="hs-operator">.|.</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">flagIf</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">tmp_opts</span><span> </span><span class="hs-operator">/=</span><span> </span><span class="hs-identifier">NoTemp</span><span class="hs-special">)</span><span>
</span><span id="line-901"></span><span>                                </span><span class="hs-comment">-- Hold data in cache for as long as possible</span><span>
</span><span id="line-902"></span><span>                                </span><span class="hs-number">256</span><span> </span><span class="hs-special">)</span><span>
</span><span id="line-903"></span><span class="hs-pragma">{-# LINE 903 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-903"></span><span>
</span><span id="line-904"></span><span>          </span><span class="hs-identifier">createFile</span><span> </span><span class="hs-identifier">devicepath</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-905"></span><span>            </span><span class="hs-identifier">withCWString</span><span> </span><span class="hs-identifier">devicepath</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">fp</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-906"></span><span>                </span><span class="hs-identifier">failIf</span><span> </span><span class="hs-special">(</span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">iNVALID_HANDLE_VALUE</span><span class="hs-special">)</span><span> </span><span class="hs-string">&quot;CreateFile&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-907"></span><span>                      </span><span class="hs-identifier">c_CreateFile</span><span> </span><span class="hs-identifier">fp</span><span> </span><span class="hs-identifier">file_access_mode</span><span>
</span><span id="line-908"></span><span>                                      </span><span class="hs-identifier">file_share_mode</span><span>
</span><span id="line-909"></span><span>                                      </span><span class="hs-identifier">nullPtr</span><span>
</span><span id="line-910"></span><span>                                      </span><span class="hs-identifier">file_open_mode</span><span>
</span><span id="line-911"></span><span>                                      </span><span class="hs-identifier">file_create_flags</span><span>
</span><span id="line-912"></span><span>                                      </span><span class="hs-identifier">nullPtr</span><span>
</span><span id="line-913"></span><span>
</span><span id="line-914"></span><span class="hs-comment">-- Tell the OS that we support skipping the request Queue if the</span><span>
</span><span id="line-915"></span><span class="hs-comment">-- IRQ can be handled immediately, e.g. if the data is in the cache.</span><span>
</span><span id="line-916"></span><span class="hs-identifier">optimizeFileAccess</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-917"></span><span class="hs-identifier">optimizeFileAccess</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-918"></span><span>    </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;SetFileCompletionNotificationModes&quot;</span><span>  </span><span class="hs-operator">$</span><span>
</span><span id="line-919"></span><span>      </span><span class="hs-identifier">c_SetFileCompletionNotificationModes</span><span> </span><span class="hs-identifier">handle</span><span>
</span><span id="line-920"></span><span>          </span><span class="hs-special">(</span><span>    </span><span class="hs-number">1</span><span>
</span><span id="line-921"></span><span class="hs-pragma">{-# LINE 921 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-921"></span><span>            </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">2</span><span class="hs-special">)</span><span>
</span><span id="line-922"></span><span class="hs-pragma">{-# LINE 922 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-922"></span><span>
</span><span id="line-923"></span><span class="hs-comment">-- Reconstruct an I/O mode from an open HANDLE</span><span>
</span><span id="line-924"></span><span class="hs-identifier">handleToMode</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">IOMode</span><span>
</span><span id="line-925"></span><span class="hs-identifier">handleToMode</span><span> </span><span class="hs-identifier">hwnd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-926"></span><span>  </span><span class="hs-identifier">mask</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_get_handle_access_mask</span><span> </span><span class="hs-identifier">hwnd</span><span>
</span><span id="line-927"></span><span>  </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">hasFlag</span><span> </span><span class="hs-identifier">flag</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">flag</span><span> </span><span class="hs-operator">.&amp;.</span><span> </span><span class="hs-identifier">mask</span><span class="hs-special">)</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">flag</span><span>
</span><span id="line-928"></span><span>  </span><span class="hs-keyword">case</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-929"></span><span>    </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">hasFlag</span><span> </span><span class="hs-special">(</span><span class="hs-number">4</span><span class="hs-special">)</span><span>                        </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">AppendMode</span><span>
</span><span id="line-930"></span><span class="hs-pragma">{-# LINE 930 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-930"></span><span>       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">hasFlag</span><span> </span><span class="hs-special">(</span><span class="hs-number">1073741824</span><span> </span><span class="hs-operator">.|.</span><span> </span><span class="hs-number">2147483648</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">ReadWriteMode</span><span>
</span><span id="line-931"></span><span class="hs-pragma">{-# LINE 931 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-931"></span><span>       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">hasFlag</span><span> </span><span class="hs-special">(</span><span class="hs-number">2147483648</span><span class="hs-special">)</span><span>                            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">ReadMode</span><span>
</span><span id="line-932"></span><span class="hs-pragma">{-# LINE 932 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-932"></span><span>       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">hasFlag</span><span> </span><span class="hs-special">(</span><span class="hs-number">1073741824</span><span class="hs-special">)</span><span>                           </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">WriteMode</span><span>
</span><span id="line-933"></span><span class="hs-pragma">{-# LINE 933 &quot;libraries\\base\\GHC\\IO\\Windows\\Handle.hsc&quot; #-}</span><span>
</span><span id="line-933"></span><span>       </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">otherwise</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">error</span><span> </span><span class="hs-string">&quot;unknown access mask in handleToMode.&quot;</span><span>
</span><span id="line-934"></span><span>
</span><span id="line-935"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;__get_handle_access_mask&quot;</span><span>
</span><span id="line-936"></span><span>  </span><span class="hs-identifier">c_get_handle_access_mask</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-937"></span><span>
</span><span id="line-938"></span><span class="hs-identifier">release</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-939"></span><span class="hs-identifier">release</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">isLockable</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-940"></span><span>               </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">ptrToWordPtr</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">h</span><span>
</span><span id="line-941"></span><span>                       </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">unlockFile</span><span> </span><span class="hs-identifier">handle</span><span>
</span><span id="line-942"></span><span>                       </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-943"></span><span>               </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-944"></span><span>
</span><span id="line-945"></span><span class="hs-comment">-- -----------------------------------------------------------------------------</span><span>
</span><span id="line-946"></span><span class="hs-comment">-- Locking/unlocking</span><span>
</span><span id="line-947"></span><span>
</span><span id="line-948"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;lockFile&quot;</span><span>
</span><span id="line-949"></span><span>  </span><span class="hs-identifier">lockFile</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CUIntPtr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CInt</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CInt</span><span>
</span><span id="line-950"></span><span>
</span><span id="line-951"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;unlockFile&quot;</span><span>
</span><span id="line-952"></span><span>  </span><span class="hs-identifier">unlockFile</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">CUIntPtr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">CInt</span><span>
</span><span id="line-953"></span><span>
</span><span id="line-954"></span><span class="hs-comment">-- | Returns -1 on error. Otherwise writes two values representing</span><span>
</span><span id="line-955"></span><span class="hs-comment">-- the file into the given ptrs.</span><span>
</span><span id="line-956"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;get_unique_file_info_hwnd&quot;</span><span>
</span><span id="line-957"></span><span>  </span><span class="hs-identifier">c_getUniqueFileInfo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-958"></span><span>
</span><span id="line-959"></span><span class="hs-comment">-- | getUniqueFileInfo assumes the C call to getUniqueFileInfo</span><span>
</span><span id="line-960"></span><span class="hs-comment">-- succeeds.</span><span>
</span><span id="line-961"></span><span class="hs-identifier">getUniqueFileInfo</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">RawHandle</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=&gt;</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Word64</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Word64</span><span class="hs-special">)</span><span>
</span><span id="line-962"></span><span class="hs-identifier">getUniqueFileInfo</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-963"></span><span>  </span><span class="hs-identifier">with</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">devptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-964"></span><span>    </span><span class="hs-identifier">with</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">inoptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-965"></span><span>      </span><span class="hs-identifier">c_getUniqueFileInfo</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">toHANDLE</span><span> </span><span class="hs-identifier">handle</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">devptr</span><span> </span><span class="hs-identifier">inoptr</span><span>
</span><span id="line-966"></span><span>      </span><span class="hs-identifier">liftM2</span><span> </span><span class="hs-special">(</span><span class="hs-special">,</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">devptr</span><span class="hs-special">)</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">inoptr</span><span class="hs-special">)</span><span>
</span><span id="line-967"></span></pre></body></html>